package cn.yeziji.forum.utils;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 对象工具类
 *
 * @author gzkemays
 * @since 2022/1/13 19:59
 */
public class ObjectUtils {
  public static boolean isNullOrEqTarget(Object obj, Object target) {
    return Objects.isNull(obj) || Objects.equals(obj, target);
  }

  public static boolean nonNullAndEqTarget(Object obj, Object target) {
    return Objects.nonNull(obj) && Objects.equals(obj, target);
  }

  public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    Map<Object, Boolean> tempSaveMap = new ConcurrentHashMap<>();
    return obj -> Objects.isNull(tempSaveMap.putIfAbsent(keyExtractor.apply(obj), Boolean.TRUE));
  }

  public static boolean objsHasNull(Object... objs) {
    for (Object obj : objs) {
      if (obj == null) {
        return false;
      }
    }
    return true;
  }
}
